using Unity.VisualScripting;
using UnityEngine;

namespace FW 
{
    public class ArtContorller : MonoBehaviour
    {

        public KeyMouseInput keyMouseInput;
        public GameObject model;
        public Transform dire;
        public Animator animator;
        public Player player ;
        public void Init(Transform parent)
        {
            model = parent.Find("render").Find("ybot").gameObject;
            animator = parent.GetComponentInChildren<Animator>();
            player = parent.GetComponent<Player>();
            keyMouseInput = player.keyInput;
            dire = parent.GetComponentInChildren<CapsuleCollider>().transform;
        }

        public void DoUpdate()
        {
            if (keyMouseInput.leftAttack || keyMouseInput.rightAttack)
            {

                if (player.onGroundSensor.isOnGround())
                {
                    //举盾
                    if (player.LeftWeponId == 0)
                    {
                        animator.SetBool("isDefense", keyMouseInput.leftAttack);
                        animator.SetLayerWeight(animator.GetLayerIndex("defense"), keyMouseInput.leftAttack ? 1 : 0);                      
                    }

                }

                //左手武器不为盾 
                if (keyMouseInput.leftAttack && player.LeftWeponId != 0)
                {
                    animator.SetBool("R0L1", true);
                    animator.SetTrigger("attack");
                }
                if (keyMouseInput.rightAttack)
                {
                    animator.SetBool("R0L1", false);
                    animator.SetTrigger("attack");
                }

            }

            if(!keyMouseInput.leftAttack && !keyMouseInput.rightAttack)
            {
                animator.SetBool("isDefense", keyMouseInput.leftAttack);
                animator.SetLayerWeight(animator.GetLayerIndex("defense"), keyMouseInput.leftAttack ? 1 : 0);
            }


            if (!keyMouseInput.isinputEnabled && keyMouseInput._isJumping) return;

            animator.SetFloat("forword", keyMouseInput.Dmag);
            if (keyMouseInput._isJumping)
            {
                animator.SetTrigger("jump");
            }

            if (keyMouseInput.isRoll)
            {
                animator.SetTrigger("roll");
            }


            if (keyMouseInput._vector3 == Vector3.zero) return;

            
            Vector3 targetForward = Vector3.Slerp(model.transform.forward, keyMouseInput._vector3, 0.1f);

            if (isGivenTag("attack"))
            {
                player.state = EPLAYERSTATE.ATK;
            }
            else if (isGivenState("roll"))
            {
                player.state = EPLAYERSTATE.roll;
            }
            else
            {
                player.state = EPLAYERSTATE.NONE;
            }
            if ((player.state != EPLAYERSTATE.ATK) && (player.state!= EPLAYERSTATE.roll))
                model.transform.forward = targetForward;
        }

        public bool isGivenState(string stateName,string layerName = "Base Layer")
        {
            var _animator = this.transform.parent.GetComponentInChildren<Animator>();
            if (_animator != null)
            {
                return _animator.GetCurrentAnimatorStateInfo(_animator.GetLayerIndex(layerName)).IsName(stateName);
            }
            return false;
        }

        public bool isGivenTag(string tagName, string layerName = "Base Layer")
        {
            var _animator = this.transform.parent.GetComponentInChildren<Animator>();
            if (_animator != null)
            {
                return _animator.GetCurrentAnimatorStateInfo(_animator.GetLayerIndex(layerName)).IsTag(tagName);
            }
            return false;
        }

        public void OnJump()
        {
            //this.transform.GetComponentInParent<Player>().keyInput.isinputEnabled = false;
            this.transform.GetComponentInParent<Player>().RestVy();
        }

        public void OnJumpFinsh()
        {
            //locakPlanar = false;
            //this.transform.GetComponentInParent<Player>().keyInput.isinputEnabled = true;
            //this.transform.GetComponentInParent<Player>().FinshJump();
            //this.transform.GetComponentInParent<Player>().keyInput.isjumpingEnabled = false;
            //this.transform.GetComponentInParent<Player>().keyInput.thrustVec = new Vector3(0, -0.5f, 0);
        }


        //这两个要改 不能用unity的收发消息 ，跟上面一样才行 
        public void OnAttack1HaEnter()
        {

            //var annimator = this.transform.parent.GetComponentInChildren<Animator>();
            //this.transform.GetComponentInParent<Player>().keyInput.isBanMove = true;    
            //annimator.SetLayerWeight(annimator.GetLayerIndex("rightAttack"), 1f);

        }


        public void OnAttackIdle()
        {
            //var annimator = this.transform.parent.GetComponentInChildren<Animator>();
            //this.transform.GetComponentInParent<Player>().keyInput.isBanMove = false;
            //annimator.SetLayerWeight(annimator.GetLayerIndex("rightAttack"), 0f);
        }
        

        public void OnUpdateDeltaPosition(object _deltapos)
        {
            if (isGivenTag("attack"))
            {
                this.transform.GetComponentInParent<Player>().deltaPosition = (Vector3)_deltapos;
            }

        }
    }
}

